package me.sdevil507.activemq.async;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.activemq.QueueName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Queue;

/**
 * 异步消息队列消息生产者
 * <p>
 * 用于处理相关耗时操作,解耦系统
 *
 * @author sdevil507
 */
@Component
@Slf4j
public class AsyncProducer {

    @Autowired
    @Qualifier("asyncQueue")
    private Queue asyncQueue;

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 异步发送消息
     *
     * @param id 唯一key
     */
    public void sendMessage(String id) {
        jmsMessagingTemplate.convertAndSend(asyncQueue, id);
    }

    /**
     * 监听消费者处理过后的异步反馈
     * <p>
     * 此处一般基于唯一key执行本地的业务逻辑操作,比如更改订单状态等
     */
    @JmsListener(destination = QueueName.ASYNC_RESULT, concurrency = "5-20")
    public void receiveMessage(String text) {
        System.out.println(text);
    }
}
